/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of Hewlett-Packard Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2013 All rights reserved. =============================
 */

package com.hp.ecs.xqa.util;

import java.math.BigDecimal;
import java.util.Date;

import com.hp.ecs.xqa.util.orm.LongKeyGenerator;



public class StringUtil {
	public static boolean isEqual(Object o1, Object o2) {
		try {
			System.out.println(LongKeyGenerator.class);
		} catch (Throwable e) {

		}
		return o1 == o2 || o1 != null && o1.equals(o2);
	}

	public static boolean isEmpty(String opt) {
		return opt == null || opt.matches("\\s*");
	}

	/**
	 * 删除str最后一个字符如果是ch则删除这个字符，通常在根据list拼装字符串的时候使用
	 * 
	 * @param str
	 * @param ch
	 */
	public static StringBuilder delLastChar(StringBuilder str, char ch) {
		if (str.length() < 1) {
			return str;
		}
		if (str.charAt(str.length() - 1) == ch) {
			str.deleteCharAt(str.length() - 1);
		}
		return str;
	}

	public static Class getVoClass(Object vo) {
		final Class clazz = vo.getClass();
		return clazz.getName().indexOf("$") > 0 ? clazz.getSuperclass() : clazz;
	}

	public static String capitalize(String s) {
		if (s == null || s.length() < 1) {
			return s;
		}
		final char c = s.charAt(0);
		if (c >= 'a' && c <= 'z') {
			return (char)(c - 'a' + 'A') + s.substring(1);
		}
		return s;
	}

	public static String unCapitalize(String s) {
		if (s == null || s.length() < 1) {
			return s;
		}
		final char c = s.charAt(0);
		if (c >= 'A' && c <= 'Z') {
			return (char)(c + 'a' - 'A') + s.substring(1);
		}
		return s;
	}

	public static <T> T convert(String s, Class<T> type) {
		if (type.equals(String.class)) {
			return (T)s;
		}

		boolean toReturnDefault = false;
		if (s == null || s.length() == 0) {
			if (type.isPrimitive()) {
				toReturnDefault = true;
			} else {
				return null;
			}
		}

		if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
			if (toReturnDefault) {
				return (T)Boolean.FALSE;
			}
			return (T)Boolean.valueOf(s);
		} else if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
			if (toReturnDefault) {
				return (T)Integer.valueOf(0);
			}
			return (T)Integer.valueOf(s);
		} else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
			if (toReturnDefault) {
				return (T)Long.valueOf(0);
			}
			return (T)Long.valueOf(s);
		} else if (type.equals(Date.class)) {
			return (T)DateUtil.parse(s, getFormat(s));
		} else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
			if (toReturnDefault && type.isPrimitive()) {
				return (T)Double.valueOf(0);
			}
			return (T)Double.valueOf(s);
		} else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
			if (toReturnDefault) {
				return (T)Float.valueOf(0);
			}
			return (T)Float.valueOf(s);
		} else if (type.equals(Character.TYPE) || type.equals(Character.class)) {
			if (toReturnDefault) {
				return (T)Character.valueOf('\0');
			}
			if (s == null) {
				return null;
			}
			return (T)Character.valueOf(s.charAt(0));
		} else if (type.equals(BigDecimal.class)) {
			return (T)new BigDecimal(s);
		}
		throw new IllegalArgumentException("Unkown Class:" + type.getName());
	}

	private static String getFormat(String dateStr) {
		StringBuilder sb = new StringBuilder();
		sb.append(dateStr.indexOf("/") > 0 ? "MM/dd/yyyy" : "yyyy-MM-dd");
		int i = dateStr.indexOf(":");
		if (i > 0) {
			if (i != dateStr.lastIndexOf(":")) {
				sb.append(" HH:mm:ss");
			} else {
				sb.append(" HH:mm");
			}
		}
		return sb.toString();
	}
}
